import {createSlice,createAsyncThunk} from '@reduxjs/toolkit'
interface stateType{
    list:itemType[]
}
interface itemType{
    url: string,
    title: string,
    id: string,
    sum:number
  }
const initialState:stateType={
    list:[]
}
export const functionList=createAsyncThunk('functionList',(item:itemType,ThunkAction)=>{
    console.log(ThunkAction)
    return item
})
const stores=createSlice({
    name:'stores',
    initialState,
    reducers:{},
    extraReducers:{
        [functionList.fulfilled.type]:(state,action)=>{
            const s=state.list.filter(v=>v.id===action.payload.id)
            if(s){
                state.list=state.list.filter(v=>{
                    if(v.id===action.payload.id){
                        return {...v,sum:v.sum+1}
                    }
                    return v
                })
            }else{
                state.list.push(action.payload)
            }
            console.log(state.list)
        }
    }
})
export default stores